import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 教师类
class Teacher {
    private String id;
    private String name;
    private String gender;
    private String department; // 新增：所属部门
    private List<Course> taughtCourses = new ArrayList<>();

    public Teacher(String id, String name, String gender, String department) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.department = department;
    }

    public void addTaughtCourse(Course course) {
        taughtCourses.add(course);
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public String getDepartment() { return department; }
    public List<Course> getTaughtCourses() { return taughtCourses; }

    @Override
    public String toString() {
        return "教师ID: " + id + ", 姓名: " + name + ", 性别: " + gender + ", 部门: " + department;
    }
}

// 学生类
class Student {
    private String id;
    private String name;
    private String gender;
    private String major; // 新增：专业
    private List<Course> selectedCourses = new ArrayList<>();

    public Student(String id, String name, String gender, String major) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.major = major;
    }

    public boolean selectCourse(Course course) {
        if (selectedCourses.contains(course)) {
            return false; // 已选该课程
        }
        selectedCourses.add(course);
        course.addStudent(this);
        return true;
    }

    public boolean dropCourse(Course course) {
        if (selectedCourses.contains(course)) {
            selectedCourses.remove(course);
            course.removeStudent(this);
            return true;
        }
        return false; // 未选该课程
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public String getMajor() { return major; }
    public List<Course> getSelectedCourses() { return selectedCourses; }

    @Override
    public String toString() {
        return "学生ID: " + id + ", 姓名: " + name + ", 性别: " + gender + ", 专业: " + major;
    }
}

// 课程类
class Course {
    private String id;
    private String name;
    private String location;
    private String time;
    private int credit; // 新增：学分
    private Teacher teacher;
    private List<Student> students = new ArrayList<>();

    public Course(String id, String name, String location, String time, int credit) {
        this.id = id;
        this.name = name;
        this.location = location;
        this.time = time;
        this.credit = credit;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
        teacher.addTaughtCourse(this);
    }

    public void addStudent(Student student) { students.add(student); }
    public void removeStudent(Student student) { students.remove(student); }

    public String getId() { return id; }
    public String getName() { return name; }
    public String getLocation() { return location; }
    public String getTime() { return time; }
    public int getCredit() { return credit; }
    public Teacher getTeacher() { return teacher; }
    public List<Student> getStudents() { return students; }

    @Override
    public String toString() {
        return "课程ID: " + id + "\n" +
                "课程名称: " + name + "\n" +
                "学分: " + credit + "\n" +
                "上课地点: " + location + "\n" +
                "上课时间: " + time + "\n" +
                "授课教师: " + (teacher != null ? teacher.getName() : "未指定");
    }
}

// 主程序类
public class DetailedCourseSystem {
    private static List<Teacher> teachers = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();
    private static List<Course> courses = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // 初始化具体数据
        initDetailedData();

        System.out.println("===== 欢迎使用学生选课系统 =====");
        showMainMenu();
    }

    // 初始化具体的课程、教师和学生数据
    private static void initDetailedData() {
        // 创建具体教师
        Teacher t1 = new Teacher("T001", "张教授", "男", "计算机科学与技术系");
        Teacher t2 = new Teacher("T002", "李老师", "女", "软件工程系");
        Teacher t3 = new Teacher("T003", "王教授", "男", "人工智能学院");
        teachers.add(t1);
        teachers.add(t2);
        teachers.add(t3);

        // 创建具体课程（含详细地点和时间）
        Course c1 = new Course("C001", "Java程序设计", "计算机楼301教室",
                "周一第1-2节(08:00-09:40), 周三第3-4节(10:10-11:50)", 3);
        Course c2 = new Course("C002", "数据结构与算法", "计算机楼202教室",
                "周二第1-2节(08:00-09:40), 周四第3-4节(10:10-11:50)", 4);
        Course c3 = new Course("C003", "人工智能导论", "实验楼101机房",
                "周五第1-2节(08:00-09:40), 周五第5-6节(14:00-15:40)", 3);
        Course c4 = new Course("C004", "数据库原理与应用", "计算机楼103教室",
                "周一第5-6节(14:00-15:40), 周三第5-6节(14:00-15:40)", 4);

        // 分配教师
        c1.setTeacher(t1);
        c2.setTeacher(t1);
        c3.setTeacher(t3);
        c4.setTeacher(t2);

        courses.add(c1);
        courses.add(c2);
        courses.add(c3);
        courses.add(c4);

        // 创建具体学生
        Student s1 = new Student("C", "张三", "男", "计算机科学与技术");
        Student s2 = new Student("S2023002", "李四", "男", "软件工程");
        Student s3 = new Student("S2023003", "王五", "女", "人工智能");
        Student s4 = new Student("S2023004", "赵六", "女", "数据科学");
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
    }

    // 显示主菜单
    private static void showMainMenu() {
        while (true) {
            System.out.println("\n===== 主菜单 =====");
            System.out.println("1. 查看所有课程 (含详细信息)");
            System.out.println("2. 学生选课");
            System.out.println("3. 学生退课");
            System.out.println("4. 查看我的选课");
            System.out.println("5. 查看课程学生名单");
            System.out.println("6. 查看教师信息");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作(0-6): ");

            try {
                int choice = Integer.parseInt(scanner.nextLine().trim());

                switch (choice) {
                    case 1:
                        showAllCourses();
                        break;
                    case 2:
                        handleCourseSelection();
                        break;
                    case 3:
                        handleCourseDrop();
                        break;
                    case 4:
                        showMyCourses();
                        break;
                    case 5:
                        showCourseStudents();
                        break;
                    case 6:
                        showAllTeachers();
                        break;
                    case 0:
                        System.out.println("谢谢使用，再见！");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选择，请输入0-6之间的数字！");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入错误，请输入数字！");
            }
        }
    }

    // 查看所有课程（含详细信息）
    private static void showAllCourses() {
        System.out.println("\n===== 所有课程详细信息 =====");
        if (courses.isEmpty()) {
            System.out.println("当前没有可用课程");
            return;
        }

        for (Course course : courses) {
            System.out.println("---------------------");
            System.out.println(course);
        }
    }

    // 查看所有教师信息
    private static void showAllTeachers() {
        System.out.println("\n===== 教师信息列表 =====");
        if (teachers.isEmpty()) {
            System.out.println("当前没有教师信息");
            return;
        }

        for (Teacher teacher : teachers) {
            System.out.println("---------------------");
            System.out.println(teacher);
            System.out.print("教授课程: ");
            if (teacher.getTaughtCourses().isEmpty()) {
                System.out.println("无");
            } else {
                for (Course course : teacher.getTaughtCourses()) {
                    System.out.print(course.getName() + " ");
                }
                System.out.println();
            }
        }
    }

    // 处理选课操作
    private static void handleCourseSelection() {
        System.out.println("\n===== 学生选课 =====");
        System.out.print("请输入你的学生ID: ");
        String studentId = scanner.nextLine().trim();

        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("错误：找不到该学生ID！");
            return;
        }

        System.out.println("欢迎，" + student.getName() + "（" + student.getMajor() + "专业）");
        System.out.print("请输入要选择的课程ID: ");
        String courseId = scanner.nextLine().trim();

        Course course = findCourseById(courseId);
        if (course == null) {
            System.out.println("错误：找不到该课程ID！");
            return;
        }

        if (student.selectCourse(course)) {
            System.out.println("选课成功！你已成功选择《" + course.getName() + "》");
            System.out.println("课程信息：" + course.getCredit() + "学分，" +
                    course.getTime() + "，" + course.getLocation());
        } else {
            System.out.println("选课失败！你已经选择过这门课程");
        }
    }

    // 处理退课操作
    private static void handleCourseDrop() {
        System.out.println("\n===== 学生退课 =====");
        System.out.print("请输入你的学生ID: ");
        String studentId = scanner.nextLine().trim();

        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("错误：找不到该学生ID！");
            return;
        }

        System.out.print("请输入要退选的课程ID: ");
        String courseId = scanner.nextLine().trim();

        Course course = findCourseById(courseId);
        if (course == null) {
            System.out.println("错误：找不到该课程ID！");
            return;
        }

        if (student.dropCourse(course)) {
            System.out.println("退课成功！你已成功退选《" + course.getName() + "》");
        } else {
            System.out.println("退课失败！你没有选择过这门课程");
        }
    }

    // 查看我的选课
    private static void showMyCourses() {
        System.out.println("\n===== 我的选课 =====");
        System.out.print("请输入你的学生ID: ");
        String studentId = scanner.nextLine().trim();

        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("错误：找不到该学生ID！");
            return;
        }

        List<Course> myCourses = student.getSelectedCourses();
        System.out.println("\n" + student.getName() + "（" + student.getMajor() + "专业）的选课情况：");

        if (myCourses.isEmpty()) {
            System.out.println("你尚未选择任何课程");
        } else {
            int totalCredit = 0;
            for (Course course : myCourses) {
                System.out.println("---------------------");
                System.out.println("课程名称: " + course.getName());
                System.out.println("课程ID: " + course.getId());
                System.out.println("学分: " + course.getCredit());
                System.out.println("上课时间: " + course.getTime());
                System.out.println("上课地点: " + course.getLocation());
                System.out.println("授课教师: " + course.getTeacher().getName());
                totalCredit += course.getCredit();
            }
            System.out.println("---------------------");
            System.out.println("已选总学分: " + totalCredit);
        }
    }

    // 查看课程学生名单
    private static void showCourseStudents() {
        System.out.println("\n===== 课程学生名单 =====");
        System.out.print("请输入课程ID: ");
        String courseId = scanner.nextLine().trim();

        Course course = findCourseById(courseId);
        if (course == null) {
            System.out.println("错误：找不到该课程ID！");
            return;
        }

        List<Student> courseStudents = course.getStudents();
        System.out.println("\n课程《" + course.getName() + "》的选课学生（共" + courseStudents.size() + "人）：");

        if (courseStudents.isEmpty()) {
            System.out.println("该课程暂无学生选课");
        } else {
            for (Student student : courseStudents) {
                System.out.println("学生ID: " + student.getId() + "，姓名: " + student.getName() +
                        "，专业: " + student.getMajor());
            }
        }
    }

    // 根据ID查找学生
    private static Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    // 根据ID查找课程
    private static Course findCourseById(String id) {
        for (Course course : courses) {
            if (course.getId().equals(id)) {
                return course;
            }
        }
        return null;
    }
}
